package com.fei.myback.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        //http://ip地址:端口/项目名/swagger-ui.html#/
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("my-site") //网站标题
                .description("my-site后台接口文档") //网站描述
                .version("1.0") //版本
                .contact(new Contact("飞少", "https://gitee.com/feiszs", "2932594554@qq.com")) //联系人
                .license("The Apache License") //协议
                .licenseUrl("http://www.baidu.com") //协议url
                .build();


        return new Docket(DocumentationType.SWAGGER_2) //swagger版本
                .pathMapping("/")
                .apiInfo(apiInfo)
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.fei.myback.controller"))
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes());
//                .securityContexts(securityContext());
    }

    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     *
     * @return
     */
    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> list = new ArrayList<>();
        list.add(new ApiKey("Bearer ", "Authorization", "header"));
        return list;
    }

    /**
     * 安全上下文
     *//*
    private List<SecurityContext> securityContext() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!myback).*$"))
                        .build());
        return securityContexts;
    }

    *//**
     * 默认的安全上引用
     *//*
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Collections.singletonList(
                new SecurityReference("Authorization", authorizationScopes));
    }*/

}
